package com.zy.mc_module.utils;

import android.text.TextUtils;
import android.view.Gravity;
import android.widget.Toast;
import com.zy.mc_module.BuildConfig;
import com.zy.mc_module.base.App;

/**
 * 统一toast弹框居中显示，加入debug辅助toast.
 * 在需要给予开发者提示的地方放使用
 */
public class ToastUtil {

    private static Toast toast;//实现不管我们触发多少次Toast调用，都只会持续一次Toast显示的时长

    private static void showToast(String msg, int duration) {
        if (!TextUtils.isEmpty(msg)) {
            //            if (toast == null) {
            //                toast = Toast.makeText(App.getAppContext(), msg, duration);
            //            } else {
            //                toast.setText(msg);
            //            }
            //            toast.setGravity(Gravity.CENTER, 0, 0);
            //            toast.show();
            try {
                if (toast != null) {
                    toast.cancel();
                }
                toast = Toast.makeText(App.getAppContext(), msg, duration);
                if (toast != null) {
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * 自定义显示位置
     * @param msg
     * @param duration
     * @param yOffset
     */
    private static void showToast(String msg, int duration, int yOffset) {
        if (toast == null) {
            toast = Toast.makeText(App.getAppContext(), msg, duration);
        } else {
            toast.setText(msg);
        }
        toast.setGravity(Gravity.CENTER, 0, yOffset);
        toast.show();
    }

    /***
     * 可以连续显示
     * @param msg
     */
    public static void show(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            toast = Toast.makeText(App.getAppContext(), msg, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    public static void showL(String msg) {
        toast = Toast.makeText(App.getAppContext(), msg, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    /**
     * 用于写代码的时候弹出一些辅助Toast,不在正式版本中弹出
     */
    public static void showDebug(String msg) {
        if (BuildConfig.DEBUG) {
            if (null != App.getAppContext()) {
                showToast(msg, Toast.LENGTH_SHORT);
            }
        }
    }

    /**
     * 短时间显示
     */
    public static void showShort(String msg, int yOffset) {
        if (null != App.getAppContext()) {
            showToast(msg, Toast.LENGTH_SHORT, yOffset);
        }
    }

    /**
     * 短时间显示
     */
    public static void showShort(String msg) {
        if (null != App.getAppContext()) {
            showToast(msg, Toast.LENGTH_SHORT);
        }
    }

    /**
     * 短时间显示
     */
    public static void showShort(int msg) {
        if (null != App.getAppContext()) {
            showToast(App.getAppContext().getResources().getString(msg), Toast.LENGTH_SHORT);
        }
    }

    /**
     * 长时间显示
     */
    public static void showLong(String msg) {
        if (null != App.getAppContext()) {
            showToast(msg, Toast.LENGTH_LONG);
        }
    }

    /**
     * 显示网络连接异常
     */
    public static void showNoNet() {
        if (null != App.getAppContext()) {
            showToast("网络连接异常，请检查网络", Toast.LENGTH_SHORT);
        }
    }
}
